package com.lst.tools.oss;

import com.lst.tools.basics.utils.RandomUtils;
import org.apache.commons.lang3.StringUtils;

/**
 * @author: Frank
 * @date: 2019/12/11 16:28
 * @version: V1.0
 */
public class OssUtils {

    private static final String SEPARATOR = "/";
    private static final int RANDOM_LENGTH = 4;

    public static String getStaticKey(String rootDirName, long userId, String format, String targetFolder) {
        StringBuffer sb = new StringBuffer();
        sb.append(rootDirName).append(SEPARATOR)
                .append(userId).append(SEPARATOR)
                .append(targetFolder).append(SEPARATOR)
                .append(System.currentTimeMillis())
                .append(RandomUtils.createRandom(RANDOM_LENGTH))
                .append(".").append(format);
        return sb.toString();
    }

    public static String getFileNameByKey(String key) {
        if (StringUtils.isBlank(key)) {
            return null;
        }
        String[] split = key.split(SEPARATOR);
        int length = split.length;
        if (length <= 1) {
            return key;
        }
        return split[length - 1];
    }
}
